package com.xinqi.modules.homework.homework.controller.app;

import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.base.response.R;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.common.satoken.utils.LoginHelper;
import com.xinqi.modules.homework.homework.dto.req.*;
import com.xinqi.modules.homework.homework.dto.rsp.CourseHomeworkResultDTO;
import com.xinqi.modules.homework.homework.dto.rsp.CourseHomeworkStudentResultDTO;
import com.xinqi.modules.homework.homework.service.CourseHomeworkService;
import com.xinqi.modules.homework.homework.service.CourseHomeworkStudentLogService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 作业基础信息APP接口
 *
 * @author: system
 * @date: 2023/03/08
 */
@Tag(name = "App-作业基础信息", description = "作业基础信息APP接口")
@RestController
@RequestMapping("/app/homework")
@RequiredArgsConstructor
public class CourseHomeworkAppController {

    @Lazy
    private final CourseHomeworkService courseHomeworkService;
    @Lazy
    private final CourseHomeworkStudentLogService courseHomeworkStudentLogService;

    /**
     * 查询课程栏目作业-分页-老师端已发布
     *
     * @Author dzy
     * @Date 10:10 2023/3/9 0009
     * @Return
     */
    @GetMapping("/page/teacher/release")
    public R<PageInfo<CourseHomeworkResultDTO>> pageTeacherRelease(Param pageable, CourseHomeworkQueryDTO dto) {
        return R.data(courseHomeworkService.pageRelease(pageable, dto));
    }

    /**
     * 查询课程栏目作业-分页-老师端未发布
     *
     * @Author dzy
     * @Date 10:10 2023/3/9 0009
     * @Return
     */
    @GetMapping("/page/teacher/no-release")
    public R<PageInfo<CourseHomeworkResultDTO>> pageTeacherNoRelease(Param pageable, CourseHomeworkQueryDTO dto) {
        return R.data(courseHomeworkService.pageNoRelease(pageable, dto));
    }

    /**
     * 查询课程栏目作业-分页-学生端
     *
     * @Author dzy
     * @Date 10:10 2023/3/9 0009
     * @Return
     */
    @GetMapping("/page/student")
    public R<PageInfo<CourseHomeworkStudentResultDTO>> pageStudent(Param pageable, CourseHomeworkStudentLogQueryDTO dto) {
        dto.setStudentId(LoginHelper.getUserId());
        PageInfo<CourseHomeworkStudentResultDTO> page = courseHomeworkStudentLogService.homeworkStudentList(pageable, dto);
        return R.data(page);
    }

    /**
     * 查询课程栏目作业-详情
     *
     * @Author dzy
     * @Date 10:10 2023/3/9 0009
     * @Return
     */
    @GetMapping("/{id}")
    public R<CourseHomeworkResultDTO> findById(@PathVariable("id") Long id, CourseHomeworkStudentDetailDTO dto) {
        return R.data(courseHomeworkService.findById(id, dto));
    }

    /**
     * 新增课程栏目作业信息
     *
     * @Author dzy
     * @Date 10:10 2023/3/9 0009
     * @Return
     */
    @PostMapping
    public R<String> insert(@Validated @RequestBody CourseHomeworkCreateDTO dto) {
        dto.setUserId(LoginHelper.getUserId());
        courseHomeworkService.insertBath(dto);
        return R.ok(MessageUtils.message("course.discuss.add.success"));
    }

    /**
     * 修改课程栏目作业信息
     *
     * @Author dzy
     * @Date 10:10 2023/3/9 0009
     * @Return
     */
    @PutMapping("/{id}")
    public R<String> update(@PathVariable("id") Long id, @Validated @RequestBody CourseHomeworkUpdateDTO dto) {
        dto.setId(id);
        courseHomeworkService.updateById(dto);
        return R.ok(MessageUtils.message("course.discuss.update.success"));
    }

    /**
     * 删除课程栏目作业信息-批量
     *
     * @Author dzy
     * @Date 10:10 2023/3/9 0009
     * @Return
     */
    @DeleteMapping
    public R delete(CourseHomeworkDeleteAllDTO dto) {
        courseHomeworkService.deleteHomeworkByIds(dto.getIds());
        return R.ok(MessageUtils.message("course.discuss.remove.success"));
    }

    /**
     * 删除课程栏目作业信息-批量 (android)
     *
     * @Author dzy
     * @Date 10:10 2023/3/9 0009
     * @Return
     */
    @GetMapping("delete")
    public R<String> deletePost(CourseHomeworkDeleteAllDTO dto) {
        courseHomeworkService.deleteHomeworkByIds(dto.getIds());
        return R.ok(MessageUtils.message("course.discuss.remove.success"));
    }

    /**
     * 作业一键发布
     *
     * @Author dzy
     * @Date 10:10 2023/3/9 0009
     * @Return
     */
    @PostMapping("release")
    public R release(@RequestBody CourseHomeworkReleaseDTO dto) {
        if (courseHomeworkService.releaseHomework(dto)) {
            return R.ok(MessageUtils.message("course.release.message.success"));
        } else {
            return R.fail(MessageUtils.message("course.release.message.fail"));
        }
    }

    /**
     * 作业一键发布-V2版本
     *
     * @Author dzy
     * @Date 10:10 2023/3/9 0009
     * @Return
     */
    @PostMapping("release/v2")
    public R releaseV2(@RequestBody CourseHomeworkReleaseDTO dto) {
        if (courseHomeworkService.releaseHomeworkV2(dto)) {
            return R.ok(MessageUtils.message("course.release.message.success"));
        } else {
            return R.fail(MessageUtils.message("course.release.message.fail"));
        }
    }


}
